package com.dgut.shopManage.entity.dto;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;

import com.dgut.shopManage.entity.Results;


public class ResultsQueryDTO {
	private String sNo;
	private String ssName;   			
	private String mmName;
	
	public String getsNo() {
		return sNo;
	}
	public void setsNo(String sNo) {
		this.sNo = sNo;
	}
	public String getSsName() {
		return ssName;
	}
	public void setSsName(String ssName) {
		this.ssName = ssName;
	}
	public String getMmName() {
		return mmName;
	}
	public void setMmName(String mmName) {
		this.mmName = mmName;
	}
	public static Specification<Results> getSpecification(ResultsQueryDTO resultsQueryDTO)
	{
		Specification<Results> spec = new Specification<Results>() {
			public Predicate toPredicate(Root<Results> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				//1.Predicate查询条件集合
				 List<Predicate> list = new ArrayList<Predicate>(); 
				 
				//2.根据 QueryDTO数据字段的值进行判断以及条件的组装
				
				 if(null != resultsQueryDTO && !StringUtils.isEmpty(resultsQueryDTO.getsNo())) {
					 Predicate  p1 =  cb.equal(root.get("sNo").as(String.class),resultsQueryDTO.getsNo());
					 list.add(p1);
				 }
				 if(null != resultsQueryDTO && !StringUtils.isEmpty(resultsQueryDTO.getSsName())) {
					 Predicate  p2 =  cb.equal(root.get("ssName").as(String.class), resultsQueryDTO.getSsName());
					 list.add(p2);
				 }
				 if(null != resultsQueryDTO && !StringUtils.isEmpty(resultsQueryDTO.getMmName())) {
					 Predicate  p3 =  cb.equal(root.get("mmName").as(String.class),resultsQueryDTO.getMmName());
					 list.add(p3);
				 }

				 //3.Predicate查询条件集合的 size 创建对应的Predicate查询条件数组
				 Predicate[] p = new Predicate[list.size()];  
				 //4.CriteriaBuilder的and 函数组装 查询条件数组
				 return cb.and(list.toArray(p));  
			}
		};
		return spec;
	}
}
